package bean;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Lytable.
 * 
 * @see bean.Lytable
 * @author MyEclipse Persistence Tools
 */

public class LytableDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(LytableDAO.class);

	// property constants
	public static final String USER_ID = "userId";

	public static final String TITLE = "title";

	public static final String CONTENT = "content";

	public void save(Lytable transientInstance) {
		log.debug("saving Lytable instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Lytable persistentInstance) {
		log.debug("deleting Lytable instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Lytable findById(java.lang.Integer id) {
		log.debug("getting Lytable instance with id: " + id);
		try {
			Lytable instance = (Lytable) getSession().get("bean.Lytable", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Lytable instance) {
		log.debug("finding Lytable instance by example");
		try {
			List results = getSession().createCriteria("bean.Lytable").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Lytable instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Lytable as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findAll() {
		log.debug("finding all Lytable instances");
		try {
			String queryString = "from Lytable";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Lytable merge(Lytable detachedInstance) {
		log.debug("merging Lytable instance");
		try {
			Lytable result = (Lytable) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Lytable instance) {
		log.debug("attaching dirty Lytable instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Lytable instance) {
		log.debug("attaching clean Lytable instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}